/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author WUKUNMENG
 * Date: Jul 5, 2010 9:23:25 AM
 */
package com.ccxe.manage.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ccxe.manage.db.CcxDB;
import com.ccxe.manage.db.DBFactory;
import com.ccxe.manage.model.MenuBean;
import com.ccxe.manage.dao.MenuManager;

/**
 * @author WUKUNMENG
 *
 */
public class MenuManagerImpl implements MenuManager{

	/**
	 * 获取所有菜单值
	 * @param id
	 * @return
	 */
	public List<MenuBean> getAllMenu(){
		CcxDB ccxDB = null;
		List<MenuBean> list = new ArrayList<MenuBean>();
		try{
			ccxDB = DBFactory.getInstance();
			ResultSet resultSet = ccxDB.executeQuery("select * from menu_item_tmp");
			while (resultSet.next()) {
				MenuBean temp = new MenuBean();
				temp.setMenuId(resultSet.getString("MENU_ID"));
				temp.setParentId(resultSet.getString("PARENT_ID"));
				temp.setIsLeaf(resultSet.getString("IS_LEAF"));
				temp.setMenuName(resultSet.getString("MENU_NM"));
				temp.setStyleFlag(resultSet.getString("STYLE_FLAG"));
				temp.setMenuURL(resultSet.getString("MENU_URL"));
				list.add(temp);
			}
			resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 根据ID获取菜单值
	 * @param id
	 * @return
	 */
	public List<MenuBean> getMenuById(String id){
		CcxDB ccxDB = null;
		List<MenuBean> list = new ArrayList<MenuBean>();
		try{
			ccxDB = DBFactory.getInstance();
			ResultSet resultSet = ccxDB.executeQuery("select * from menu_item_tmp where MENU_ID=?",new Object[]{id} );
			while (resultSet.next()) {
				MenuBean temp = new MenuBean();
				temp.setMenuId(resultSet.getString("MENU_ID"));
				temp.setParentId(resultSet.getString("PARENT_ID"));
				temp.setIsLeaf(resultSet.getString("IS_LEAF"));
				temp.setMenuName(resultSet.getString("MENU_NM"));
				temp.setStyleFlag(resultSet.getString("STYLE_FLAG"));
				temp.setMenuURL(resultSet.getString("MENU_URL"));
				list.add(temp);
			}
			resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 获取根节点
	 * @param id
	 * @return
	 */
	public List<MenuBean> getRootMenu(){
		CcxDB ccxDB = null;
		List<MenuBean> list = new ArrayList<MenuBean>();
		try{
			ccxDB = DBFactory.getInstance();
			ResultSet resultSet = ccxDB.executeQuery("select * from menu_item_tmp where PARENT_ID = 0");
			while (resultSet.next()) {
				MenuBean temp = new MenuBean();
				temp.setMenuId(resultSet.getString("MENU_ID"));
				temp.setParentId(resultSet.getString("PARENT_ID"));
				temp.setIsLeaf(resultSet.getString("IS_LEAF"));
				temp.setMenuName(resultSet.getString("MENU_NM"));
				temp.setStyleFlag(resultSet.getString("STYLE_FLAG"));
				temp.setMenuURL(resultSet.getString("MENU_URL"));
				list.add(temp);
			}
			resultSet.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 添加菜单Bean
	 * @param menuBean
	 */
	public void addMenuBean(MenuBean menuBean){
		CcxDB ccxDB = null;
		try{
			ccxDB = DBFactory.getInstance();
			String sql = "insert into menu_item_tmp (MENU_ID,PARENT_ID,IS_LEAF,MENU_NM,STYLE_FLAG,MENU_URL)values(MENU_ITEM_TMP_SEQ.Nextval,?,?,?,?,?)";
			PreparedStatement statement = ccxDB.getConnection().prepareStatement(sql);
			statement.setString(1, menuBean.getParentId());
			statement.setString(2, menuBean.getIsLeaf());
			statement.setString(3, menuBean.getMenuName());
			statement.setString(4, menuBean.getStyleFlag());
			statement.setString(5, menuBean.getMenuURL());
			statement.execute();
			statement.execute("update menu_item_tmp set IS_LEAF='0' where MENU_ID=" + menuBean.getParentId());
			statement.execute();
			statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 更新菜单Bean
	 * @param menuBean
	 */
	public void updateMenuBean(MenuBean menuBean){
		CcxDB ccxDB = null;
		try{
			ccxDB = DBFactory.getInstance();
			String sql = "update menu_item_tmp set PARENT_ID=?,MENU_NM=?,STYLE_FLAG=?,MENU_URL=? where MENU_ID=?";
			PreparedStatement statement = ccxDB.getConnection().prepareStatement(sql);
			statement.setString(1, menuBean.getParentId());
			statement.setString(2, menuBean.getMenuName());
			statement.setString(3, menuBean.getStyleFlag());
			statement.setString(4, menuBean.getMenuURL());
			statement.setString(5, menuBean.getMenuId());
			statement.execute();
			statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取当前ID值
	 * @return id
	 */
	public int getCurrId() {
		CcxDB ccxDB = null;
		int id = 0;
		try{
			ccxDB = DBFactory.getInstance();
			String sql = "select max(menu_id) from menu_item_tmp";
			PreparedStatement statement = ccxDB.getConnection().prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				id = resultSet.getInt(1);
			}
			resultSet.close();
			statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return id;
	}
	
	/**
	 * 更新菜单Bean
	 * @param menuBean
	 */
	public void deleteMenuBean(MenuBean menuBean){
		CcxDB ccxDB = null;
		try{
			ResultSet resultSet = null;
			ccxDB = DBFactory.getInstance();
			String sql = "delete from menu_item_tmp where MENU_ID=?";
			PreparedStatement statement = ccxDB.getConnection().prepareStatement(sql);
			statement.setString(1, menuBean.getMenuId());
			statement.execute();
			statement.close();
			//判断子节点
			statement = ccxDB.getConnection().prepareStatement("select * from menu_item_tmp where PARENT_ID=" + menuBean.getParentId());
			resultSet = statement.executeQuery("select * from menu_item_tmp where PARENT_ID=" + menuBean.getParentId());
			if(!resultSet.next()){
				statement.execute("update menu_item_tmp set IS_LEAF='1' where MENU_ID=" + menuBean.getParentId());
			}
			resultSet.close();
			statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
